/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

/**
 *
 * @author TIT
 */
import javax.naming.NamingException;
import util.Constants;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import session.AdminFacadeRemote;
import session.EmployeeFacadeRemote;

public class loginControll {

    public static AdminFacadeRemote getAdminFacade() throws NamingException {
        return util.Facade.getAdminFacade();
    }

    public static void processAction(HttpServletRequest request) throws NamingException, Exception {
        String action = request.getParameter("action");
        if(Constants.LOGIN_ADMIN.equalsIgnoreCase(action)) {
            loginAdmin(request);
        } else if(Constants.LOGIN_USER.equalsIgnoreCase(action)) {
            loginUser(request);
        }
    }

    static void loginAdmin(HttpServletRequest request) throws NamingException {
        try {
            HttpSession session = request.getSession(true);

            Controller.log.info("Begin loginAdmin");
            String userAdmin = request.getParameter("name");
            Controller.log.info(userAdmin);
            String pass = request.getParameter("password");
            Controller.log.info(pass);
            AdminFacadeRemote adminBean = getAdminFacade();

            if (adminBean.find(userAdmin) != null) {

                Controller.log.info("dung user");

                if (adminBean.find(userAdmin).getPassword() == null ? pass == null : adminBean.find(userAdmin).getPassword().equals(pass)) {

                    session.setAttribute("admin", userAdmin);
                    session.setAttribute("subPage", "");

                    Controller.log.info("userAdmin : " + session.getAttribute("admin"));
                }
            }
            Controller.log.info("userAdmin : " + session.getAttribute("admin"));
            Controller.log.info("subPage : " + session.getAttribute("subPage"));
            Controller.log.info("Login successfully......");
        } catch (Exception ex) {
            Controller.log.error("Error loginAdmin: " + ex.getMessage());
        }
    }
    
    static void loginUser(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession(true);

            Controller.log.info("Begin loginAdmin");
            String user = request.getParameter("name");
            Controller.log.info(user);
            String pass = request.getParameter("password");
            Controller.log.info(pass);
            EmployeeFacadeRemote emBean = util.Facade.getEmployeeFacade();

            Controller.log.info("dung user");
            model.Employee em = emBean.selectByShortName(user);
            if(em != null) {
                if((em.getPassword()==null && pass==null) || pass.equals(em.getPassword())) {
                    session.setAttribute("user", em.getEmployeeID().toString());
                    session.setAttribute("subPage", Constants.DEFECTLOG_STATISTIC);
                    session.setAttribute("pageName", Constants.DEFECT_LOG_PAGE_NAME);
                }
            } else {
                Controller.sendMessage(request, "Account not exists.", true);
            }
            
            Controller.log.info("user: " + session.getAttribute("user"));
            Controller.log.info("subPage : " + session.getAttribute("subPage"));
            Controller.log.info("Login successfully......");
        } catch (Exception ex) {
            Controller.log.error("Error loginUser: " + ex.getMessage());
        }
    }
}
